<?php

namespace App\Admin\Forms;

use App\Models\System;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class ApplicationSetting extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '应用设置';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        if(!empty($request->post('reward'))){
            $system = System::where('id' , 999)->first();

            $system->reward = $request->post('reward');

            if($system->save()){
                admin_success('修改成功');
            }else{
                admin_error('修改失败');
            }
        }

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->slider('reward', __('返现百分比'))->options([
            'skin'=> "round",
            'grid'=> true,
            'max' => 100,
            'min' => 0,
            'step' => 1,
            'postfix' => '%',
        ])->setWidth(5)->rules('required|integer|min:0|max:100' , [
            'required' => '返现百分比 不能为空',
            'integer' => '返现百分比 必须是整数',
            'min' => '返现百分比 不能小于0',
            'max' => '返现百分比 不能大于100',
        ]);
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        return [
            'reward' => System::where('id' , 999)->value('reward'),
        ];
    }
}
